import { WEB_TITLE } from '@/config'
import defaultSettings from '@/settings'
import { useDark, useToggle } from '@vueuse/core'

// 强制设置为白色主题（默认）
localStorage.setItem('vueuse-color-scheme', 'light')
document.documentElement.classList.remove('dark')

const isDark = ref(false) // 强制写死为白色主题
const toggleDark = () => {
  isDark.value = !isDark.value
  if (isDark.value) {
    document.documentElement.classList.add('dark')
    localStorage.setItem('vueuse-color-scheme', 'dark')
  } else {
    document.documentElement.classList.remove('dark')
    localStorage.setItem('vueuse-color-scheme', 'light')
  }
}

const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings

const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || ''

const useSettingsStore = defineStore('settings', {
  state: () => ({
    title: '',
    theme: storageSetting.theme || '#409EFF',
    sideTheme: 'theme-light', // 强制写死为白色侧边栏
    showSettings: showSettings,
    topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
    tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
    fixedHeader: storageSetting.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
    sidebarLogo: storageSetting.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
    dynamicTitle: storageSetting.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle,
    isDark: false // 强制写死为白色主题
  }),
  actions: {
    // 修改布局设置
    changeSetting(data) {
      const { key, value } = data
      if (this.hasOwnProperty(key)) {
        this[key] = value
      }
    },
    // 设置网页标题
    setTitle(title) {
      this.title = title
      if (this.dynamicTitle) {
        document.title = title + ' - ' + WEB_TITLE
      } else {
        document.title = WEB_TITLE
      }
    },
    // 切换暗黑模式
    toggleTheme() {
      toggleDark()
      this.isDark = isDark.value
    }
  }
})

export default useSettingsStore
